<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket连接测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .status {
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
            font-weight: bold;
        }
        .connected {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .disconnected {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
        #messageLog {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 10px;
            height: 300px;
            overflow-y: auto;
            margin-top: 20px;
            font-family: monospace;
            font-size: 12px;
        }
        .message {
            margin: 5px 0;
            padding: 5px;
            border-left: 3px solid #007bff;
            background-color: white;
        }
        .error {
            border-left-color: #dc3545;
            color: #dc3545;
        }
        .success {
            border-left-color: #28a745;
            color: #28a745;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>WebSocket连接测试</h1>
        
        <div id="status" class="status disconnected">
            状态: 未连接
        </div>
        
        <div>
            <button onclick="connect()" id="connectBtn">连接WebSocket</button>
            <button onclick="disconnect()" id="disconnectBtn" disabled>断开连接</button>
            <button onclick="sendAuth()" id="authBtn" disabled>发送认证</button>
            <button onclick="testNotification()" id="testBtn" disabled>测试通知</button>
        </div>
        
        <div>
            <h3>连接信息</h3>
            <p>WebSocket地址: <code>ws://localhost:8080/ws</code></p>
            <p>用户类型: <select id="userType">
                <option value="Student_Auth">学生端</option>
                <option value="PC_Auth">管理员端</option>
            </select></p>
        </div>
        
        <div>
            <h3>消息日志</h3>
            <div id="messageLog"></div>
        </div>
    </div>
    
    <script>
        let ws = null;
        let isConnected = false;
        
        function updateStatus(connected, message) {
            const statusDiv = document.getElementById('status');
            const connectBtn = document.getElementById('connectBtn');
            const disconnectBtn = document.getElementById('disconnectBtn');
            const authBtn = document.getElementById('authBtn');
            const testBtn = document.getElementById('testBtn');
            
            isConnected = connected;
            
            if (connected) {
                statusDiv.className = 'status connected';
                statusDiv.textContent = '状态: 已连接 - ' + message;
                connectBtn.disabled = true;
                disconnectBtn.disabled = false;
                authBtn.disabled = false;
                testBtn.disabled = false;
            } else {
                statusDiv.className = 'status disconnected';
                statusDiv.textContent = '状态: 未连接 - ' + message;
                connectBtn.disabled = false;
                disconnectBtn.disabled = true;
                authBtn.disabled = true;
                testBtn.disabled = true;
            }
        }
        
        function addMessage(message, type = 'info') {
            const logDiv = document.getElementById('messageLog');
            const time = new Date().toLocaleTimeString();
            const messageDiv = document.createElement('div');
            messageDiv.className = 'message ' + type;
            messageDiv.innerHTML = `<strong>[${time}]</strong> ${message}`;
            logDiv.appendChild(messageDiv);
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        function connect() {
            try {
                const wsUrl = 'ws://localhost:8080/ws';
                addMessage(`正在连接到: ${wsUrl}`);
                
                ws = new WebSocket(wsUrl);
                
                ws.onopen = function(event) {
                    updateStatus(true, '连接成功');
                    addMessage('WebSocket连接已建立', 'success');
                };
                
                ws.onmessage = function(event) {
                    addMessage(`收到消息: ${event.data}`, 'success');
                    try {
                        const data = JSON.parse(event.data);
                        addMessage(`解析后的消息: ${JSON.stringify(data, null, 2)}`, 'success');
                    } catch (error) {
                        addMessage(`消息解析失败: ${error.message}`, 'error');
                    }
                };
                
                ws.onclose = function(event) {
                    updateStatus(false, '连接已关闭');
                    addMessage(`WebSocket连接已关闭，代码: ${event.code}, 原因: ${event.reason}`, 'error');
                };
                
                ws.onerror = function(error) {
                    updateStatus(false, '连接错误');
                    addMessage(`WebSocket连接错误: ${error}`, 'error');
                };
                
            } catch (error) {
                addMessage(`连接失败: ${error.message}`, 'error');
                updateStatus(false, '连接失败');
            }
        }
        
        function disconnect() {
            if (ws) {
                ws.close();
                ws = null;
                updateStatus(false, '手动断开');
                addMessage('手动断开WebSocket连接');
            }
        }
        
        function sendAuth() {
            if (!ws || ws.readyState !== WebSocket.OPEN) {
                addMessage('WebSocket未连接，无法发送认证', 'error');
                return;
            }
            
            const userType = document.getElementById('userType').value;
            const authMessage = {
                userId: 'test_user_' + Date.now(),
                messageType: userType,
                title: '',
                content: '',
                param: ''
            };
            
            ws.send(JSON.stringify(authMessage));
            addMessage(`发送认证消息: ${JSON.stringify(authMessage)}`);
        }
        
        function testNotification() {
            const title = '测试通知';
            const content = '这是一条来自测试页面的通知';
            
            fetch('/api/test/websocket/notify-students', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    title: title,
                    content: content
                })
            })
            .then(response => response.json())
            .then(data => {
                addMessage(`测试通知API响应: ${JSON.stringify(data)}`, 'success');
            })
            .catch(error => {
                addMessage(`测试通知失败: ${error.message}`, 'error');
            });
        }
    </script>
</body>
</html> 